﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using NTTDATA.BEENChina.ResultCertification.Main.ProcessLog;
using NTTDATA.BEENChina.ResultCertification.model;
using Timer = System.Timers.Timer;
using System.Linq;

namespace NTTDATA.BEENChina.ResultCertification.UI
{
    public partial class FormMonitor : Form
    {
        private Timer monitorTimer;
        

        public FormMonitor()
        {
            InitializeComponent();

            CheckForIllegalCrossThreadCalls = false;

           

            monitorTimer = new Timer(5 * 1000);
            monitorTimer.Elapsed += new System.Timers.ElapsedEventHandler(monitorTimer_Elapsed);
            monitorTimer.Start();
           
        }

        private int countRow = 50;

        private string GetMaxIdAtTextBox()
        {
            if (textBox1.Text == null)
                return string.Empty;

            int maxLine = textBox1.Lines.Count();
            if (textBox1.Lines.Any())
            {
                string id = textBox1.Lines[maxLine - 1 ];

                id = id.Split(new string[] {"-"}, StringSplitOptions.None)[0];

                if (Util.StrHelper.IsNumeric(id))
                {
                    return id;
                }
                else
                {
                    return string.Empty;
                }

            }

            return string.Empty;
        }


        void monitorTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
 
            try{
                using (ProcessLogClient client = new ProcessLogClient())
                {
                    List<ProcessLogItem> logItems = null;
                    string maxId = GetMaxIdAtTextBox();

               
                        // maxId = string.Empty;
                        if (String.IsNullOrEmpty(maxId))
                        {
                            logItems = client.GetAllProcessLogs();
                        }
                        else
                        {
                            logItems = client.GetMaxProcessLogs(maxId);
                        }

                        if (logItems != null)
                        {
                           // textBox1.Text = string.Empty;

                            int i = 0;
                            foreach (var processLogItem in logItems.OrderBy(t => int.Parse( t.Id)))
                            {
                                i++;
                                if (i> countRow)
                                    break;
                        

                                ShowMessage(string.Format("{3}-{2}  {0} {1}", 
                                    processLogItem.ProcessModel.ToString(CultureInfo.InvariantCulture),
                                    processLogItem.Message,
                                    processLogItem.UpdateTime,
                                    processLogItem.Id));
                            }
                            ;
                        }

               

                
                }
            }
            catch (Exception ex)
            {
                Log.Console.Error(" monitor err:" ,ex);
            }
        }


 
        private readonly int _maxLineCount = 200;

        public void ShowMessage(string msg)
        { 
            if (textBox1.Lines.Length > _maxLineCount)
            {
                int c = 0;
                for (int i = 0; i < _maxLineCount / 2; i++)
                    c += textBox1.Lines[i].Length + Environment.NewLine.Length;

                textBox1.Text = textBox1.Text.Substring(c);
            }

            textBox1.AppendText(Environment.NewLine + msg);
        }

        private void FormMonitor_FormClosing(object sender, FormClosingEventArgs e)
        {
            monitorTimer.Elapsed -= new System.Timers.ElapsedEventHandler(monitorTimer_Elapsed);
            this.monitorTimer.Stop();
        }

       
    }
}
